package ltd.dujiabao.reserver.service;

import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.reserver.constant.Constant;
import ltd.dujiabao.reserver.util.OcrClientUtils;
import ltd.dujiabao.reserver.util.StringUtils;

/**
 * @author : DuJiabao
 * @Project : reserver
 * @Time : 2021/11/6 0:34
 * @Desc : 通过腾讯云的ocr，完成验证码的识别工作
 */
@Service
@Slf4j
public class OcrService {
    /**
     * @param url 被识别图片的url
     * @return 识别结果
     */
    public String doOcr(String url) {
        if (StringUtils.isNullOrEmpty(url)) {
            log.warn("Get null or empty url while recognizing.");
            return null;
        }

        String code = OcrClientUtils.doOcr(url, Constant.secretId, Constant.secretKey,
                Constant.ocrEndpoint, Constant.regionName);
        if (!StringUtils.isNullOrEmpty(code)) {
            log.info("Recognizing the validation code file successfully, code: {}", code);
            return code;
        }
        log.warn("Recognizing the validation code file fail.");
        return null;
    }
}

